var featuretteTabs = (function(){
	return{
		init: function(){
			// Add the following widgets to the dynamic loader.
			// By doing this, when a widget is added to a sidebar, its javascript listeners will be initialized
			OPW.dynamicLoader.add('featurette_tabs',featuretteTabs.registerTriggers);
			OPW.dynamicLoader.add('tabbed_content',featuretteTabs.registerTriggers);
			OPW.dynamicLoader.add('content_body',featuretteTabs.registerTriggers);
			OPW.dynamicLoader.add('archives_index',featuretteTabs.registerTriggers);
		},
		
		/**
		 * Setup event listeners
		**/
		registerTriggers: function(){
			jQuery('a.op_add_tab_trigger').click(featuretteTabs.addTab);
			jQuery('a.op_remove_tab_trigger').click(featuretteTabs.removeTab);
			//jQuery('a.op_column_select_trigger').click(featuretteTabs.chooseColumns); // deprecated
			jQuery(':input.sortby_extra_trigger').change(featuretteTabs.filterBy);
			jQuery(':input.op_content_body_add_comp').change(featuretteTabs.addDiscreteTab);
			jQuery('div.op_current_tabs').each(function(){jQuery(this).sortable({
					//axis: 'y', // commented out for jquery compat
					revert:true,
					opacity: .70,
					delay: 100
				})});
				
			// listen to toggle for show/hide advanced options
			jQuery('a.op_advanced_options_trigger').click(featuretteTabs.toggleAdvanced);
		},
		
		
		addTab: function(e){
			var target = e.srcElement || e.currentTarget, // get click event
				container = jQuery(target).parents().find('div.tab_options').eq(0), // get the container element for the tab list
				clone = jQuery(container).clone().show(); // make a copy of tab_options to append to list
				
			container.siblings('div.op_current_tabs').eq(0).append(clone);
			
			// Setup listeners for new links and input
			jQuery('a.op_remove_tab_trigger').click(featuretteTabs.removeTab);
			// jQuery('a.op_column_select_trigger').click(featuretteTabs.chooseColumns); // deprecated
			jQuery(':input.sortby_extra_trigger').change(featuretteTabs.filterBy);
			
			return false; // Return false to prevent #
		},
		
		removeTab: function(e){
			if(confirm('Remove tab from widget?')){ // Confirm before removing tab from widget
				var target = e.srcElement || e.currentTarget; // get click event
				jQuery(target).parent().remove() // remove the tab relative to the click event
			}
			return false; // Return false to prevent #
		},
		
		/**
		 * Used by tabbed_content widget.
		 * Upon selecting a content type, a second dropdown appears to provide a sorting option.
		 * this is the logic that shows that extra option
		 **/
		filterBy: function(e){
			var target = e.srcElement || e.currentTarget, // get click event
				container = jQuery(target).parents().find('span.sort_options').children('span.sort_options_'+target.value).eq(0),
				clone = jQuery(container).clone().show(); // assign and show 'sort by' options
			
			// Remove old, extra "sort by" options if they're there.
			if(elem = jQuery(target).parent().find('span.sort_options_child').eq(0)){ // span.sort_options_child is the span that contains the extra dropdown
				elem.remove(); // remove
			}
			jQuery(target).parent().append(clone); // add new "sort by" options to tab_options div
		},
		
		// deprecated
		/*chooseColumns: function(e){
			var target = e.srcElement || e.currentTarget; // get click event
			jQuery(target).next().get(0).value = jQuery(target).hasClass('split') ? 2 : 1;
			jQuery(target).toggleClass('split'); 
			return false; // Return false to prevent #
		},*/
		
		/**
		 * Show/Hide options using jQuery toggle()
		 */
		toggleAdvanced: function(e){
			var target = e.srcElement || e.currentTarget, // get the click event
				options = null; // will store options div to this var
				
			options = jQuery(target).siblings('div.op_advanced_options'); // get the options div relative to click event
			options.toggle();
			return false; // Return false to prevent #
		}
	}
})();

jQuery(document).ready(featuretteTabs.init);